<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 权限管理
 */

namespace Admin\Controller;
use Model\OrderModel;
use Model\UserModel;
use Model\OrderExtModel;

class ExchangeController extends BaseController{
    public $oModel;
    public $uModel;
    public $oeModel;
    public function __construct(){
        parent::__construct();
        $this->oModel = new OrderModel();
        $this->uModel = new UserModel();
        $this->oeModel = new OrderExtModel();
    }
    //兑换记录
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['orderno']  = @$_POST['orderno'] ? $_POST['orderno'] : @$sessionParam['orderno'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['order_type'] = 1;
        $where['status'] = ['in',[2,3]];
        if($sessionArr['name']){
            $uwhere['_string'] = "tel='{$sessionArr['name']}' or real_name='{$sessionArr['name']}'";
            $uRes = $this->uModel->findData($uwhere,'Id');
            $where['uid'] = $uRes['id'] ? $uRes['id'] : 0;
        }
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['orderno']){
            $where['order_no'] = $sessionArr['orderno'];
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $resList = $this->oModel->search($where,$page,$this->pernum);
        $totalNum = $this->oModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        $uidArr = [];
        $oidArr = [];
        foreach ($resList as $val){
            $uidArr[] = $val['uid'];
            $oidArr[] = $val['id'];
        }
        $uRes = [];
        $oeRes = [];
        if($uidArr){
            $uList = $this->uModel->selectData(['id'=>['in',$uidArr]]);
            foreach ($uList as $val){
                $uRes[$val['id']]['tname'] = $val['tel'];
                $uRes[$val['id']]['rname'] = $val['real_name'];
            }
            $oeList = $this->oeModel->selectData(['oid'=>['in',$oidArr]]);
            foreach ($oeList as $val){
                $val['goods_sku'] = json_decode($val['goods_sku'],true);
                $val['rcp_sku'] = json_decode($val['rcp_sku'],true);
                $oeRes[$val['oid']] = $val;
            }
        }

        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $this->assign('oeRes',$oeRes);
        $this->display('exchange/index');
    }
    //添加物流单号
    public function addExpress(){
        $this->oModel->editData(['id'=>$_POST['oid']],['status'=>3]);
        $this->oeModel->editData(['oid'=>$_POST['oid']],['express_name'=>$_POST['expressName'],'express_no'=>$_POST['expressNo'],'send_time'=>date('Y-m-d H:i:s')]);
        $this->printOut(['code'=>0]);
    }
}